<?php
namespace app\common\model;




use app\common\logic\UserExtend;
use think\Db;

class Order extends Base
{

    public static $status = [
        0  => ['name' => '代付款'     , 'id' => 0],
        1  => ['name' => '待确定'     , 'id' => 1],
        2  => ['name' => '待开始'     , 'id' => 2],
        3  => ['name' => '进行中'     , 'id' => 3],
        4  => ['name' => '已完成'     , 'id' => 4],
        5  => ['name' => '已评论'     , 'id' => 5],
        6  => ['name' => '已取消'     , 'id' => 6],
        7  => ['name' => '申请退款中'  , 'id' => 7],
        8  => ['name' => '退款驳回'    , 'id' => 8],
        9  => ['name' => '申述中'      , 'id' => 9],
        10 => ['name' => '申述失败'    , 'id' => 10],
        11 => ['name' => '已退款'      , 'id' => 11],
        12 => ['name' => '已过期'      , 'id' => 12],
        13 => ['name' => '后台自动取消' , 'id' => 13]
    ];
    public static $pay_way = [
        1  => ['name' => '钻石'     , 'id' => 1],
        2  => ['name' => '支付宝'     , 'id' => 2],
        3  => ['name' => '微信'     , 'id' => 3],

    ];

    public function userTo()
    {
        return $this->hasOne(Users::class,'user_id','to_user');
    }
    public function skill()
    {
        return $this->hasOne(Skill::class,'skill_id','skill_id');
    }

    public function userToExtend()
    {
        return $this->hasOne(UserExtend::class,'user_id','to_user');
    }

    public function getStatusTextAttr($value,$data)
    {
        return self::$status[$data['status']]['name'];
    }

    public function getPayWayTextAttr($value,$data)
    {
        return self::$pay_way[$data['pay_way']]['name'];
    }

    protected function initialize()
    {
        parent::initialize();

    }

    public function getList($map = []){
        return $this->alias('a')
            ->join('skill s','s.skill_id = a.skill_id','LEFT')
            ->join('skill_form f','f.form_id = a.form_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->where($map)
            ->order('a.update_time')
            ->field('a.*,s.skill_name,f.form_name,u.nick_name,u.uuid')
            ->paginate('',false,['query'=>request()->param()]);
    }


    public function getOrder($map = [])
    {
        return $this->alias('a')
            ->join('skill s','s.skill_id = a.skill_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->join('users us','us.user_id = a.to_user','LEFT')
            ->where($map)
            ->order('a.create_time desc')
            ->field('a.*,s.skill_name,u.nick_name,us.nick_name as u_name,u.uuid,us.uuid as sd')
            ->paginate('',false,['query'=>request()->param()]);
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 获取订单评论
     */
    public function getComment($map = [])
    {
        $rows =  $this->alias('a')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->join('gift g','g.gift_id = a.gift_id','LEFT')
            ->where($map)
            ->order('a.update_time desc')
            ->field('a.score,a.anonymous,g.gift_name,g.thumbnail,g.img,a.num,a.tags,a.content,a.update_time,u.nick_name,u.header_img,u.user_id')
            ->cache(30)
            ->paginate();
        $items = $rows->items();

        foreach ($items as $k => $item){
            $tags = [];
            if ($item['anonymous']){
                $items[$k]['nick_name'] = '匿名用户';
                $items[$k]['header_img'] = config('default_img');
            }
            $tags['tag']    = ['in',$item['tags']];
            $tags['status'] = 1;
            $items[$k]['tags'] = Db::name('skill_tag')->where($tags)->field('tag_name')->cache(30)->select();
            $items[$k]['user_id'] = hashid($item['user_id']);
            $items[$k]['formatTime'] = formatTime($item['update_time'],false);
        }
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$items];
    }


    public static function InterestTag($map = [],$as = false)
    {

        if ($as){
            $field = 's.skill_name as tag';
        }else{
            $field = 's.skill_name';
        }
        return self::alias('a')
            ->where($map)
            ->join('skill s','s.skill_id = a.skill_id','LEFT')
            ->distinct(true)
            ->field($field)
            ->cache(15)
            ->select();
    }

    public function getRows($map = [],$join = [])
    {
        $rows =  $this->alias('a')
            ->join($join)
            ->where($map)
            ->field('a.order_id,a.score,a.price,a.status,a.create_time,a.num,u.nick_name,u.header_img,u.user_id,sf.form_name,s.skill_name,s.spec')
            ->cache(3)
            ->order('a.order_id desc')
            ->paginate()->each(function ($item){

                $item['user_id'] = hashid($item['user_id']);

            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }

    public function getOne($map = [])
    {

        $join = [
            [ 'skill_form sf','sf.form_id = a.form_id','LEFT'],
            [ 'skill s','s.skill_id = a.skill_id','LEFT'],
            [ 'gift g','g.gift_id = a.gift_id','LEFT'],
        ];

        return $this->alias('a')
            ->join($join)
            ->where($map)
            ->field('a.tags,a.order_id,a.user_id,a.to_user,a.remark,a.order_time,a.num,g.gift_name,g.img,g.thumbnail,a.content,a.score,a.price,a.status,a.create_time,sf.form_name,s.skill_name,s.spec,a.reason,s.img as skill_img')
//            ->cache(3)
            ->find();
    }

    public function getDetail($map = [])
    {
        $join = [
            [ 'skill_form sf','sf.form_id = a.form_id','LEFT'],
            [ 'skill s','s.skill_id = a.skill_id','LEFT'],
            [ 'gift g','g.gift_id = a.gift_id','LEFT'],
        ];

        return $this->alias('a')
            ->join($join)
            ->where($map)
            ->field('a.*,g.gift_name,g.img ,g.thumbnail as gift_img,sf.form_name,s.skill_name,s.img as skill_img,s.spec')
            ->find();
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 评价界面信息
     */
    public function evaluateInfo($map = [])
    {
        $join = [
            [ 'skill s','s.skill_id = a.skill_id','LEFT'],
            [ 'users u','u.user_id = a.to_user','LEFT'],
            [ 'user_extend e','e.user_id = a.user_id','LEFT'],
        ];

        $field = 'a.order_id,s.skill_name,a.order_time,a.num,s.spec,s.tag,u.nick_name,u.header_img,e.noble_id,e.noble_time,a.user_id';

        return $this->alias('a')
            ->join($join)
            ->where($map)
            ->field($field)
            ->find();
    }

}